<?php

namespace App\Services;

class AesService
{
    /**
     * 加密
     * @param $input
     * @param $key
     * @return string
     */
    public static function encrypt($input, $key)
    {
        $key = substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true), 0, 16);
        $data = openssl_encrypt($input, 'AES-128-ECB', $key, OPENSSL_RAW_DATA);

        return base64_urlsafe_encode($data);
    }

    /**
     * 解密
     * @param $input
     * @param $key
     * @return false|string
     */
    public static function decrypt($input, $key)
    {

        $key = substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true), 0, 16);
        $encrypted = base64_urlsafe_decode($input);

        return openssl_decrypt($encrypted, 'AES-128-ECB', $key, OPENSSL_RAW_DATA);
    }
}
